package com.hp.service.iml;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hp.dao.GoodsDao;
import com.hp.dao.ItemsDao;
import com.hp.dao.OrderDao;
import com.hp.dao.UsersDao;
import com.hp.model.Goods;
import com.hp.model.Items;
import com.hp.model.Order;
import com.hp.model.Users;
import com.hp.service.OrderService;

@Service
public class OrderServiceiml implements OrderService{
	
	@Autowired
	private UsersDao udao;
	@Autowired
	private GoodsDao gdao;
	@Autowired
	private ItemsDao idao;
	@Autowired
	private OrderDao odao;

	@Override
	public List<Order> findAllOrder() {
		List<Order> list=new ArrayList<Order>();
		list= odao.findAllOrder();//查询全部
		for (Order order : list) {
			
			System.out.println(order);
			//查找对应的用户
			int uid=order.getUserId();//用户id			
			Users user= udao.findUserById(uid);
			order.setUser(user);
			
			//通过订单信息查找对应的items
			int oid=order.getId();
			List<Items> itemList =  idao.findByOrderId(oid);
			
			for (Items item : itemList) {
				//查找对应的商品
				int gId=item.getGoodId();
				Goods good = gdao.findById(gId);
				item.setGoods(good);				
			}
			
			//把items存入order中
			order.setItems(itemList);
			
		}		
		//返回结果
		return list;
	}

	@Override
	public List<Order> nopayOrder() {
		return odao.nopayOrder();
	}

	@Override
	public List<Order> payOrder() {
		return odao.payOrder();
	}

	@Override
	public List<Order> Ordering() {
		return odao.Ordering();
	}

	@Override
	public List<Order> finishOrder() {
		return odao.finishOrder();
	}

	@Override
	public Integer delorder(int id) {
		return odao.delorder(id);
	}

	@Override
	public Integer orderUpdate(Integer id) {
		return odao.orderUpdate(id);
	}

	@Override
	public Integer completeorder(Integer id) {
		return odao.completeorder(id);
	}

	//分页查询
	@Override
	public List<Order> findOrderBypage(Integer pageSize,Integer page) {
		
		int m=(page-1)*pageSize;
		
		
		return odao.findOrderBypage(pageSize,m);
	}

	@Override
	public Integer count() {
		return odao.count();
	}

}
